package com.ilop.sthome.ui.fragment.device;

import android.content.Intent;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.example.common.base.BasePresentFragment;
import com.example.common.base.MyPagerAdapter;
import com.example.common.utils.DisplayUtils;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.event.EventGatewayInfo;
import com.ilop.sthome.data.event.EventSynchronization;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.device.DeviceContract;
import com.ilop.sthome.mvp.present.device.DevicePresenter;
import com.ilop.sthome.ui.activity.config.ProductActivity;
import com.ilop.sthome.ui.activity.device.GatewayMainActivity;
import com.ilop.sthome.ui.activity.device.RoomManageActivity;
import com.ilop.sthome.view.onScrollCallBack;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.FragmentDeviceBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2020/7/13.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备页面
 */
public class DeviceFragment extends BasePresentFragment<DevicePresenter, FragmentDeviceBinding> implements DeviceContract.IView, onScrollCallBack {

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_device;
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresenter = new DevicePresenter(mContext, mHandler);
    }

    @Override
    protected void initView() {
        super.initView();
        List<Fragment> mFragment = new ArrayList<>();
        String[] mTitle = new String[]{getString(R.string.device), getString(R.string.room_i)};
        mFragment.add(DeviceForTypeFragment.newInstance(this));
        mFragment.add(DeviceForRoomFragment.newInstance(this));
        MyPagerAdapter mAdapter = new MyPagerAdapter(getChildFragmentManager());
        mAdapter.setTitleAndFragment(mFragment);
        mDBind.deviceViewPager.setAdapter(mAdapter);
        mDBind.deviceTabLayout.addTab(mTitle[0]);
        mDBind.deviceTabLayout.addTab(mTitle[1]);
        mDBind.deviceTabLayout.setupWithViewPager(mDBind.deviceViewPager);
        mDBind.deviceRefresh.setProgressViewEndTarget(true, DisplayUtils.dip2px(mContext, 100));
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getGatewayList();
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_EVENT_GATEWAY_INFO, EventGatewayInfo.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: gateway " +  event.getData_str2());
                String roomId = event.getData_str2().substring(0, 2);
                mPresenter.synchronizationSubDevice(event.getDeviceName(), roomId);
            }
        });

        LiveDataBus.get().with(CommonId.KEY_EVENT_SYNCHRONIZATION, EventSynchronization.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: synchronization" + event.getState());
                if (event.getState() == EventSynchronization.SYN_DEVICE){
                    mPresenter.synchronizationNickName(event.getDeviceName());
                }else if (event.getState() == EventSynchronization.SYN_FINISH){
                    mPresenter.synchronizationFinish(event.getDeviceName());
                }
            }
        });

        LiveDataBus.get().with(CommonId.REFRESH_CAMERA, String.class).observe(this, s -> {
            Log.i(TAG, "initDataBus: refresh camera" + s);
            if (s!=null){
                mPresenter.getRoomListByAccount();
            }
        });

        LiveDataBus.get().with(CommonId.DEVICE_ITEM_CLICK, DeviceInfoBean.class).observe(this, deviceInfoBean -> {
            if (deviceInfoBean !=null) {
                if (!TextUtils.isEmpty(deviceInfoBean.getProductKey())) {
                    Intent intent = new Intent(mContext, GatewayMainActivity.class);
                    intent.putExtra(CommonId.KEY_DEVICE_NAME, deviceInfoBean.getDeviceName());
                    startActivity(intent);
                } else {
                    mPresenter.clickChildItem(deviceInfoBean);
                }
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.mainOpenCenter.setOnClickListener(v -> LiveDataBus.get().with(CommonId.OPEN_MENU).setValue(0));
        mDBind.mainRoomEdit.setOnClickListener(v -> skipAnotherActivity(RoomManageActivity.class));
        mDBind.deviceAdd.setOnClickListener(() -> skipAnotherActivity(ProductActivity.class));
        mDBind.deviceRefresh.setOnRefreshListener(() -> {
            mPresenter.getGatewayList();
            mDBind.deviceRefresh.setRefreshing(false);
        });
        mDBind.deviceViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mDBind.deviceTabLayout.getTabLayout()));
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.refreshViews();
    }

    @Override
    public void deviceListIsEmpty(boolean isEmpty) {
        mDBind.deviceAdd.setVisibility(isEmpty ? View.GONE : View.VISIBLE);
    }

    @Override
    public void skipActivity(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void showMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void onScrollDeviceType(boolean enable) {
        mDBind.deviceRefresh.setEnabled(enable);
    }

    @Override
    public void onScrollDeviceRoom(boolean enable) {
        mDBind.deviceRefresh.setEnabled(enable);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.viewOnDestroy();
    }
}
